import time

from selenium import webdriver
from selenium.webdriver.common.devtools.v137.device_access import select_prompt
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from ranzhiporductTest.porduct import locator


# 定义一个函数，获取浏览器驱动
def getDriver(driverStr):
    if driverStr == "safari":
        driver = webdriver.Safari()
    elif driverStr == "chrome":
        driver = webdriver.Chrome()
    elif driverStr == "firefox":
        driver = webdriver.Firefox()
    else:
        driver = webdriver.Edge()

    return driver
# 定义一个类，封装不同的操作
class BasePage:
    def __init__(self,driver):
        self.driver = driver
    # 打开网页
    def open(self,url):
        self.driver.get(url)
        # 窗口最大化
        self.driver.maximize_window()
        # 隐式等待
        self.driver.implicitly_wait(5)
    # 退出浏览器
    def quit(self):
        self.driver.close()
    #定位元素
    def locate_element(self,target):
        try:
            # 处理target字符串
            list = target.split("=")
            locator = (list[0].strip(),list[1].strip())
            # 显性等待
            element = WebDriverWait(self.driver, 5, 0.5).until(
                expected_conditions.presence_of_element_located(locator))
            return element
        except Exception as e:
            print("定位失败",e)
    # 发送信息
    def send_information(self,target,text):
        #向指定元素发送文本
        element = self.locate_element(target)
        if element is not None:
            print("-----------")
            try:
                # 清空输入框再发送（可选操作，根据实际需求调整）
                element.clear()
                element.send_keys(text)
                print(f"向元素{target}发送文本成功:{text}")
            except Exception as e:
                print(f"向元素{target}发送文本成功{e}")
    # 点击信息
    def click_element(self,target):
        #点击指定元素
        try:
            element = self.locate_element(target)
            element.click()
            print(f"点击元素{target}成功")
        except Exception as e:
            print(f"点击元素{target}失败:{e}")
    # 跳转到ifname
    def swith_to_ifname(self,target):
        #切换到指定iframe
        try:
            iframe_element = self.locate_element(target)
            self.driver.switch_to.frame(iframe_element)
            print(f"切换到iframe {target} 成功")
        except Exception as e:
            print(f"切换到iframe {target} 失败: {e}")
    # 返回上一层
    def swith_to_parent_frame(self):
        try:
            self.driver.switch_to.parent_frame()
            print("返回上一层iframe成功")
        except Exception as e:
            print(f"返回上一层iframe失败:{e}")
    #返回到最外层
    def switch_to_default_content(self):
        #返回到最外层页面（退出所有iframe）
        try:
            self.driver.switch_to.default_content()
            print("返回到最外层页面成功")
        except Exception as e:
            print(f"返回最外层页面失败:{e}")
    # 根据下标进行选择下拉
    def select_by_index(self,target,index):
        # 通过下标选择下拉框选项
        try:
            select_element = self.locate_element(target)
            select = Select(select_element)
            select.select_by_index(index)
            print(f"下拉框 {target} 通过下标 {index} 选择成功")
        except Exception as e:
            print(f"下拉框 {target} 通过下标选择失败: {e}")
if __name__ == '__main__':
    #创建基础类对象
    bp = BasePage(getDriver("edge"))
    bp.open("http://www.sogou.com")
    # 定位元素
    bp.locate_element("id = query")
    time.sleep(10)
    # 发送
    bp.send_information("id = query","selenium")
    bp.click_element("id = stb")
    time.sleep(100)